package com.erp.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.dao.ForumDAO;
import com.erp.dao.TopicDAO;
import com.erp.domain.TForum;
import com.erp.dto.Forum;
import com.erp.dto.Page;
import com.erp.mapping.Status;

@Service(value = "forumService")
@Transactional
public class ForumService {

	private ForumDAO forumDAO = null;
	private TopicDAO topicDAO = null;

	public ForumDAO getForumDAO() {
		return forumDAO;
	}

	@Resource(name = "forumDAO")
	public void setForumDAO(ForumDAO forumDAO) {
		this.forumDAO = forumDAO;
	}

	public TopicDAO getTopicDAO() {
		return topicDAO;
	}

	@Resource(name = "topicDAO")
	public void setTopicDAO(TopicDAO topicDAO) {
		this.topicDAO = topicDAO;
	}

	public void add(Forum forum) {
		TForum tForum = new TForum(forum);
		forumDAO.save(tForum);
		tForum.setOrderNumber(tForum.getId());
	}

	public boolean delete(Forum forum) {
		forumDAO.deleteById(forum.getId());
		// TODO 彻底删除引用Forum的类的对应字段
		return true;
	}

	/**
	 * 设置Forum状态设为已删除，同时将引用Forum的类的状态设为已删除
	 * 
	 * @param forum
	 * @return
	 */
	public boolean setStatusDeleted(Forum forum) {
		forumDAO.getById(forum.getId()).setStatus(Status.STATUS_DELETED);
		// TODO 将引用Forum的类的状态设为已删除
		return true;
	}

	/**
	 * 将版块状态恢复为正常，同时将引用Forum的类的状态恢复为正常
	 * 
	 * @param forum
	 * @return
	 */
	public boolean setStatusNormal(Forum forum) {
		forumDAO.getById(forum.getId()).setStatus(Status.STATUS_NORMAL);
		// TODO 将引用Forum的类的状态恢复为正常
		return true;
	}

	public void update(Forum forum) {
		TForum tForum = forumDAO.getById(forum.getId());
		tForum.setName(forum.getName());
		tForum.setDescription(forum.getDescription());
	}

	/**
	 * 上移，最上面的不能继续上移
	 */
	public void moveUp(Forum forum) {
		TForum tForum = forumDAO.getById(forum.getId());
		TForum lastForum = forumDAO
				.getFirst("from TForum forum where forum.orderNumber < "
						+ tForum.getOrderNumber()
						+ " order by forum.orderNumber desc");

		// 如果已经移到顶部
		if (lastForum != null) {
			int temp = tForum.getOrderNumber();
			tForum.setOrderNumber(lastForum.getOrderNumber());
			lastForum.setOrderNumber(temp);
		}
	}

	/**
	 * 下移，最下面的不能下移
	 */
	public void moveDown(Forum forum) {
		TForum tForum = forumDAO.getById(forum.getId());
		TForum nextForum = forumDAO
				.getFirst("from TForum forum where forum.orderNumber > "
						+ tForum.getOrderNumber()
						+ " order by forum.orderNumber");

		// 如果已经移到底部
		if (nextForum != null) {
			int temp = tForum.getOrderNumber();
			tForum.setOrderNumber(nextForum.getOrderNumber());
			nextForum.setOrderNumber(temp);
		}
	}

	public Forum getForumById(Integer id) {
		return new Forum(forumDAO.getById(id));
	}

	public List<Forum> listAll() {
		List<Forum> forumList = new ArrayList<Forum>(0);
		List<TForum> TForumList = new ArrayList<TForum>(0);
		TForumList = forumDAO.list("from TForum forum where forum.status="
				+ Status.STATUS_NORMAL.ordinal()
				+ "  order by forum.orderNumber");
		if (TForumList != null) {
			for (TForum temp : TForumList) {
				forumList.add(new Forum(temp));
			}
		}
		return forumList;
	}

	/**
	 * 获取版块回收站列表
	 * 
	 * @return
	 */
	public List<Forum> getRecycleList() {
		List<Forum> forumRecycleList = new ArrayList<Forum>(0);
		List<TForum> TForumRecycleList = new ArrayList<TForum>(0);
		TForumRecycleList = forumDAO
				.list("from TForum forum where forum.status="
						+ Status.STATUS_DELETED.ordinal()
						+ "  order by forum.orderNumber");
		if (TForumRecycleList != null) {
			for (TForum temp : TForumRecycleList) {
				forumRecycleList.add(new Forum(temp));
			}
		}
		return forumRecycleList;
	}

	public List<Forum> getPagedList(Page page) {
		// 开始页码
		int from = (page.getPageIndex() - 1) * page.getPageSize();
		List<Forum> forumList = null;
		int totalRecord = forumDAO.countAll().intValue();
		page.setTotalRecord(totalRecord);
		int totalPage = 1;
		if (totalRecord > 0) {
			forumList = new ArrayList<Forum>();
			List<TForum> tForumList;
			String hql = "from TForum forum where forum.status="
					+ Status.STATUS_NORMAL.ordinal();
			tForumList = forumDAO.paging(hql, from, page.getPageSize());
			// 设置总页数
			totalPage = totalRecord % (page.getPageSize()) > 0 ? totalRecord
					/ (page.getPageSize()) + 1 : totalRecord
					/ (page.getPageSize());

			for (int i = 0; i < tForumList.size(); i++) {
				forumList.add(new Forum(tForumList.get(i)));
			}
		}
		page.setTotalPage(totalPage);
		return forumList;
	}

}
